package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.config.R;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.entity.SysConfigInfo;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-23
 */
@RestController
@RequestMapping("/sys_config")
public class SysConfigController {

    @Autowired
    private ISysConfigService sysConfigService;

    @GetMapping("/all")
    public R<Map> all(){
        //map下有4个键值对list , allData ,configForm ,configRules
        HashMap<String, Object> map = new HashMap<>();

        //2.allData
        ArrayList<Map> arrayList = new ArrayList<>();

        List<SysConfig> sysConfigList = sysConfigService.list();
        List<SysConfigInfo> allData = new ArrayList<>();

        for (SysConfig sysConfig : sysConfigList) {

            SysConfigInfo sysConfigInfo = new SysConfigInfo();
            BeanUtils.copyProperties(sysConfig,sysConfigInfo);

            if (sysConfig.getId()==125){
                HashMap<String, String> hashMap1 = new HashMap<>();
                hashMap1.put("labe1","是");
                hashMap1.put("value","1");
                HashMap<String, String> hashMap2 = new HashMap<>();
                hashMap2.put("label","否");
                hashMap2.put("value","2");
                arrayList.add(hashMap1);
                arrayList.add(hashMap2);
                sysConfigInfo.setConfigValue(arrayList);
            }
            allData.add(sysConfigInfo);

        }

        //1.list

        ArrayList<Object> list = new ArrayList<>();
        //1.基础配置
        HashMap<Object, Object> baseMap = new HashMap<>();
        baseMap.put("name","基础配置");
        baseMap.put("key", "base");
        ArrayList<Object> BaseChildren = new ArrayList<>();
        //1.1.1站点配置
        HashMap<Object, Object> baseMap1 = new HashMap<>();
        baseMap1.put("name","站点配置");
        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfig::getGroupValue,"站点配置");
        List<SysConfig> sysConfigList1 = sysConfigService.list(wrapper);
        List<SysConfigInfo> dataList = new ArrayList<>();

        for (SysConfig sysConfig : sysConfigList1) {
            SysConfigInfo sysConfigInfo = new SysConfigInfo();
            BeanUtils.copyProperties(sysConfig,sysConfigInfo);
            if (sysConfig.getId()==125) {
                sysConfigInfo.setConfigValue(arrayList);
            }
            dataList.add(sysConfigInfo);

        }

        baseMap1.put("data",dataList);
        //1.1.2底部信息
        HashMap<Object, Object> baseMap2 = new HashMap<>();
        baseMap2.put("name","底部信息");
        LambdaQueryWrapper<SysConfig> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SysConfig::getGroupValue,"底部信息");
        baseMap2.put("data",sysConfigService.list(wrapper1));
        //1.1.3其他配置
        HashMap<Object, Object> baseMap3 = new HashMap<>();
        baseMap3.put("name","其他配置");
        LambdaQueryWrapper<SysConfig> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(SysConfig::getGroupValue,"其他配置");
        baseMap3.put("data",sysConfigService.list(wrapper2));
        BaseChildren.add(baseMap1);
        BaseChildren.add(baseMap2);
        BaseChildren.add(baseMap3);
        baseMap.put("children",BaseChildren);
        //1.2系统设置
        HashMap<Object, Object> systemMap = new HashMap<>();
        systemMap.put("name","系统设置");
        systemMap.put("key","system");
        //1.2.1管理系统配置
        HashMap<Object, Object> systemMap1 = new HashMap<>();
        systemMap1.put("name","管理系统配置");
        LambdaQueryWrapper<SysConfig> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(SysConfig::getGroupValue,"管理系统配置");
        systemMap1.put("data",sysConfigService.list(wrapper3));
        ArrayList<Object> systemChildren = new ArrayList<>();
        systemChildren.add(systemMap1);
        systemMap.put("children",systemChildren);
        //1.3其他设置
        HashMap<Object, Object> customMap = new HashMap<>();
        customMap.put("name","其他设置");
        customMap.put("key","custom");
        //1.3.1  通知公告
        HashMap<Object, Object> customMap1 = new HashMap<>();
        customMap1.put("name","通知公告");
        LambdaQueryWrapper<SysConfig> wrapper4 = new LambdaQueryWrapper<>();
        wrapper4.eq(SysConfig::getGroupValue,"通知公告");
        customMap1.put("data",sysConfigService.list(wrapper4));

        //1.3.2 前台图片
        HashMap<Object, Object> customMap2 = new HashMap<>();
        customMap2.put("name","前台图片");
        LambdaQueryWrapper<SysConfig> wrapper5 = new LambdaQueryWrapper<>();
        wrapper5.eq(SysConfig::getGroupValue,"前台图片");
        customMap2.put("data",sysConfigService.list(wrapper5));
        ArrayList<Object> customChildren = new ArrayList<>();
        customChildren.add(customMap1);
        customChildren.add(customMap2);
        customMap.put("children",customChildren);

        list.add(baseMap);
        list.add(systemMap);
        list.add(customMap);



        //3.configForm
        HashMap<Object, Object> configForm = new HashMap<>();
        for (SysConfig sysConfig : sysConfigList) {
            configForm.put(sysConfig.getAttrKey(),sysConfig.getAttrValue());
        }

        //4.configRules
        HashMap<Object, Object> configRules = new HashMap<>();
        configRules.put("site_name","[{\"required\":true,\"message\":\"站点名必须\",\"trigger\":\"blur\"}]");

        map.put("list",list);
        map.put("allData",allData);
        map.put("configForm",configForm);
        map.put("configRules",configRules);

        return R.success(map);

    }

    @PostMapping("/update")
    public R<String>update(List<SysConfig>list){

        for (SysConfig sysConfig : list) {
            sysConfigService.updateById(sysConfig);
        }

        return R.success("修改配置成功");

    }
    @PostMapping("/add")
    public ResultData saveSysConfig(@RequestBody SysConfig sysConfig) {
        //1.添加成功
        sysConfigService.save(sysConfig);
        return ResultData.ok("成功", sysConfig);
    }

}

